<?php
/**
 * chotrois functions
 * @author: HoanVX
 * @version $Id: functions.php 73 2015-01-18 05:20:09Z voxuanhoan@gmail.com $
 */

/**
 * Check client browser is less than IE mode 9 (1 to 8)
 * @return boolean
 */
function isLtIE8() {
	if(preg_match('/(?i)msie [1-8]/',$_SERVER['HTTP_USER_AGENT'])) {
		return true;  }
	else
		return false;
}

/**
 * Setup and enable some default feature
 */
function register_chotrois_setup() {

	// Regist menu for wp admin
	register_nav_menus(
	array('header-menu' 		=> __( 'Header menu ' )
		, 'header-menu-right' 	=> __("Header's menu right")
		, 'footer-menu' 		=>__('Footer menu'))
	);

	// Add option post thumbnail for page and post in backend
	add_theme_support( 'post-thumbnails' );

}
add_action( 'init', 'register_chotrois_setup' );

///////////
// Add Style tag and script tag

/**
 * Add a new css file into a page is loaded
 * @param String $cssName: file name is put at place which it's called
 *  EX: css/page/regist.css
 */
function add_style($css) {

	if (!empty($css)) {

		// Make handle name auto belong to file name is passed over argurment
		$handle = "chotroi_stylesheet_";
		$handle .= basename($css, ".css");
		wp_register_style($handle, get_template_directory_uri() . "/" . $css);
		wp_enqueue_style($handle);
	}
}
add_action('wp_enqueue_scripts', 'add_style');

/**
 * Add a new css file into header page is loaded
 * @param String $cssName: file name is put at place which it's called
 *  EX: js/page/regist.js
 */
function add_script($script) {
	if (!empty($script)) {

		// Make handle name auto belong to file name is passed over argurment
		$handle = "chotroi_script_";
		$handle .= basename($script, ".js");
// 		wp_register_script($handle, get_template_directory_uri() . "/" . $script);
		wp_enqueue_script($handle, get_template_directory_uri() . "/" . $script, array('jquery'));
	}
}
add_action('wp_enqueue_scripts', 'add_script');


// END add style
//////////////////////

/**
 * Ko tự động generate thêm 2 hình khi attach hinh vao bai post
 * @param unknown $sizes
 * @return unknown
 */
function ct_filter_image_sizes( $sizes) {

	unset( $sizes['thumbnail']);
	unset( $sizes['medium']);
	unset( $sizes['large']);

	return $sizes;
}
add_filter('intermediate_image_sizes_advanced', 'ct_filter_image_sizes');

// Include phần xử lý của ajax của post tin rao vặt
require_once get_template_directory() .'/page/post-process.php';

/** Pagination */
/**
 * Paginator function</br>
 * It's used to paging the category pages
 * @param string $pages
 * @param number $range
 */
function pagination($pages = '', $range = 4) {
	$showitems = $range + 1;

	global $paged;
	if(empty($paged))
		$paged = 1;

	if($pages == '') {
		global $wp_query;
		$pages = $wp_query->max_num_pages;
		if(!$pages) {
			$pages = 1;
		}
	}

	if(1 != $pages) {
		echo "<nav>
				<ul class='pagination'>";
		if($paged > 1 ) {
			echo '<li><a href="'. get_pagenum_link(1) .'" aria-label="Về trang đâu"><span aria-hidden="true">Về trang đâu</span></a></li>';
			echo "<li><a href='". get_pagenum_link($paged - 1). "' >&lsaquo;</a></li>";
		} else {
			echo '<li><a href="javascript:void();" aria-label="Về trang đâu"><span aria-hidden="true">Về trang đâu</span></a></li>';
			echo "<li><a href='javascript:void(0);' >&lsaquo;</a></li>";
		}

		for ($i=1; $i <= $pages; $i++) {
			if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
				echo ($paged == $i) ? "<li class=\"active\"><a href='javascript:void();'>".$i."</li>"
									: "<li><a href='".get_pagenum_link($i)."' >".$i."</a></li>";
			}
		}

		if ($paged < $pages ) {
			echo "<li><a href='".get_pagenum_link($paged + 1)."' >&rsaquo;</a></li>";
			echo "<li class='last-page'><a href='".get_pagenum_link($pages)."'>Trang cuối</a></li>";
		} else {
			echo "<li><a hre='javascript:void(0);'>&rsaquo;</a></li>";
			echo "<li class='last-page'><a hre='javascript:void(0);'>Trang cuối</a></li>";
		}
		echo "</ul>
			</nav>\n";
	}
}
?>
